#!/bin/bash
DDATE=$(date +%s)
usage()
{
cat << EOF
$0 : Creates gnuplot plots from parsed file

Usage: $0 <option> file-to-plot

OPTIONS: to plot:
  -d 		drive speeds
  -n 		network speeds
  -b 		network buffers
  -r		recpoints
  -c		CPU usage
  -m 		Used virtual memory
  -f <folder> 	Alternate folder to save plots as images
  -o <file>	Make into a tex output file
EOF
}
THEPLOT=()
#time hd-speed netspeed b_free b_busy b_loaded r_free r_busy <stream_0> ...
n=1
while [ $# -gt 0 ]
do
  #Arg1 can come at any time so doing this the boring way
  case $1 in
    -d) 
      THEPLOT+=(HD)
      ;;
    -n)
      THEPLOT+=(NET)
      ;;
    -b)
      THEPLOT+=(NB)
      ;;
    -r)
      THEPLOT+=(RB)
      ;;
    -c)
      THEPLOT+=(CPU)
      ;;
    -m)
      THEPLOT+=(MEM)
      ;;
    -f)
      #Create the outfolder if it doesn't exist
      EXTRAPARAM="$EXTRAPARAM
      set terminal epslatex color colortext
      "
      ;;
    -o)
      GENTOFILE=$2
      shift
      ;;
    -t)
      #EXTRAPARAM="$EXTRAPARAM
      #set title '$2'
      #"
      ;;
    -*) echo "Unknown argument $1";usage;exit 1; echo "Errors in parameters $0";;
    #File is the extra out
  *) eval "arg_$n=$1";n=$(($n+1));;
  esac
  shift
done

if [ ${#THEPLOT[@]} -eq 0 ]
then
  usage
  exit -1
fi


FILE=$arg_1

if [ -z "$GENTOFILE" ]
then
  exit -1
fi



for i in ${THEPLOT[@]}
do
  #Format extra parameters for outputting files
  if [ ! -z "$EXTRAPARAM" ]
  then 
    EOUT="$EXTRAPARAM
    set output \"${FILE#*/}.tex\"
    "
  fi
  #echo $EOUT
  case $i in 
    HD)
      gnuplot -persist <<-EOF
      set title "HD-speed"
      set xlabel "Time(s)"
      set ylabel "Mb/s"
      $EOUT
      plot "$FILE" using 1:2 with line title "HD-speed"
EOF
      ;;
    NET)
      THREADS=($(cat $FILE | head -n 1|cut -b 62-))
      #echo ${THREADS[@]}
      kk=0
      for dodo in ${THREADS[@]}
      do
	ADDPLOT="$ADDPLOT,\"$FILE\" using 1:$((9+$kk)) with line title \"$dodo\""
	kk=$(($kk+1))
      done
      echo " set title \"Network speed\"
      set xlabel \"Time(s)\"
      set ylabel \"Mb/s\"
      $EOUT
      plot \"$FILE\" using 1:3 with line title \"Total\"$ADDPLOT
      " > $GENTOFILE
      ;;
    NB)
      gnuplot -persist <<-EOF
      set title "Memory buffers"
      set xlabel "Time(s)"
      set ylabel "N"
      $EOUT
      plot "$FILE" using 1:4 with line title "Free mem" lt rgb "green", "$FILE" using 1:5 with line title "Busy mem" lt rgb "red", "$FILE" using 1:6 with line title "Loaded mem" lt rgb "blue","$FILE" using 1:7 with line title "Free rec", "$FILE" using 1:8 with line title "Busy rec"
EOF
      ;;
    CPU)
      gnuplot -persist <<-EOF
      set title "Cpu used"
      set xlabel "Time(s)"
      set ylabel "percent"
      $EOUT
      plot "$FILE" using 1:2 with line title "User cpu", "$FILE" using 1:3 with line title "system cpu"
EOF
      ;;
    MEM)
      gnuplot -persist <<-EOF
      set title "Virtual memory used"
      set xlabel "Time(s)"
      set ylabel "Bytes"
      $EOUT
      plot "$FILE" using 1:4 with line title "VSIZE"
EOF
      ;;
    RB)
      gnuplot -persist <<-EOF
      set title "Recpoints"
      set xlabel "Time(s)"
      set ylabel "N"
      $EOUT
      plot "$FILE" using 1:7 with line title "Free", "$FILE" using 1:8 with line title "Busy"
EOF
      ;;
  esac
done
